﻿using SuperX.Common;
using SuperX.Common.APIManager;
using SuperX.Helper;
using SuperX.Helper.ServiceResponse;

namespace SuperX.WebAppService
{
    [PythonScript(label = "WebAppServiceController", apply = "WebAppServiceController", type = "keyword", detail = "")]

    public class WebAppServiceController : DynamicApiController
    {
        private readonly WebAppServer _server;

        public WebAppServer Server { get; set; }

        public WebAppServiceController(WebAppServer server)
        {
            _server = server;
            Name = nameof(WebAppServiceController);
        }

        [WebApi("获取Webapp配置文件", "", "")]
        public ApiResponse<WebAppConfig> GetWebAppConfig()
        {
            return _server.Config.ToResponse();
        }

        [WebApi("设置Webapp配置文件", "", "")]
        public ApiResponse<string> SetWebAppConfig(SysConfigModel[] models)
        {
            var config = _server.Config;
            foreach (var item in models)
            {
                ConfigReflectionHelper.UpdateValueByPath(config, item.Key, item.Value);
            }
            _server.Save();
            return ApiResponse<string>.Success();
        }
    }
}